package com.example.myapplication.ui.activity


import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.example.myapplication.R
import com.example.myapplication.ui.fragment.HomeFragment
import com.example.myapplication.ui.fragment.MineFragment
import com.example.myapplication.ui.fragment.ProjectFragment
import com.example.myapplication.ui.fragment.SquareFragment
import com.kun.base.adapter.MyFragmentPagerAdapter
import com.kun.base.ui.AbsBaseActivity
import com.kun.base.ui.AbsBaseFragment
import kotlinx.android.synthetic.main.activity_main.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView.OnPagerTitleChangeListener

/**
 * 主页
 */
class MainActivity : AbsBaseActivity() {
    private val titles = arrayOf("首页", "广场", "项目", "我的")
    private val images = arrayOf(
        R.drawable.home_icon,
        R.drawable.square_icon,
        R.drawable.project_icon,
        R.drawable.mine_icon
    )
    private lateinit var adapter: MyFragmentPagerAdapter

    private lateinit var fragments: ArrayList<AbsBaseFragment>
    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initViewModel() {
    }

    override fun initView() {
        fragments = ArrayList()
        fragments.add(HomeFragment())
        fragments.add(SquareFragment())
        fragments.add(ProjectFragment())
        fragments.add(MineFragment())
        adapter = MyFragmentPagerAdapter(supportFragmentManager, fragments, titles.asList())

        view_pager.adapter = adapter
        view_pager.offscreenPageLimit = 3
        initMagicIndicator()
    }

    private fun initMagicIndicator() {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            @SuppressLint("InflateParams")
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {

                val commonPagerTitleView = CommonPagerTitleView(context)
                // load custom layout
                val customLayout =
                    LayoutInflater.from(context).inflate(R.layout.simple_pager_title_layout, null)
                val titleImg =
                    customLayout.findViewById<View>(R.id.title_img) as ImageView
                val titleText =
                    customLayout.findViewById<View>(R.id.title_text) as TextView

                titleImg.setImageResource(images[index])
                titleText.text = titles[index]
                commonPagerTitleView.setContentView(customLayout)

                commonPagerTitleView.onPagerTitleChangeListener = object :
                    OnPagerTitleChangeListener {
                    override fun onSelected(index: Int, totalCount: Int) {
                        titleText.setTextColor(Color.parseColor("#333333"))
                        titleImg.isSelected = true
                    }

                    override fun onDeselected(index: Int, totalCount: Int) {
                        titleText.setTextColor(Color.parseColor("#999999"))
                        titleImg.isSelected = false
                    }

                    override fun onLeave(
                        index: Int,
                        totalCount: Int,
                        leavePercent: Float,
                        leftToRight: Boolean
                    ) {
                    }

                    override fun onEnter(
                        index: Int,
                        totalCount: Int,
                        enterPercent: Float,
                        leftToRight: Boolean
                    ) {
                    }
                }

                commonPagerTitleView.setOnClickListener {
                    view_pager.currentItem = index
                }

                return commonPagerTitleView
            }

            override fun getCount(): Int {
                return fragments.size
            }

            override fun getIndicator(context: Context?): IPagerIndicator? {
                return null
            }

        }
        magic_indicator.navigator = commonNavigator
        ViewPagerHelper.bind(magic_indicator, view_pager)
    }
}